import MD5 from 'js-md5'
import store from '../store'
export default {
  AnonToken: function (timestamp) { // 14
    return store.state.ApiForm + MD5(timestamp + '&' + store.state.ApiKey).toUpperCase() // 用户登录前
  },
  AuthToken: function (timestamp, userKey) {
    return store.state.ApiForm + MD5(timestamp + '&' + store.state.ApiKey + '&' + userKey).toUpperCase() // 用户登录
  },
  getQueryString: function (name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    var r = window.location.search.substr(1).match(reg)
    if (r != null) return unescape(r[2])
    return null
  },
  // 生成随机串
  randomString: function (len) {
    len = len || 32
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
    var maxPos = $chars.length
    var pwd = ''
    for (var i = 0; i < len; i++) {
      pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
    }
    return pwd
  },
  // 导出敏感词模板
  exportRaw: function (name, data) {
    var urlObject = window.URL || window.webkitURL || window
    var exportBlob = new Blob([data])
    var saveLink = document.createElementNS(
      'http://www.w3.org/1999/xhtml',
      'a'
    )
    saveLink.href = urlObject.createObjectURL(exportBlob)
    saveLink.download = name
    this.fakeClick(saveLink)
  },
  fakeClick (obj) {
    var ev = document.createEvent('MouseEvents')
    ev.initMouseEvent(
      'click',
      true,
      false,
      window,
      0,
      0,
      0,
      0,
      0,
      false,
      false,
      false,
      false,
      0,
      null
    )
    obj.dispatchEvent(ev)
  }
}
